﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.Vendors
{
    public class VendorcontactUpdateHandler : IRequestHandler<VendorcontactUpdateCommand,Result>
    {
        private readonly IBaseRepository<Vendorcontact> _vendorcontactRepository;

        public VendorcontactUpdateHandler(IBaseRepository<Vendorcontact> vendorcontactRepository)
        {
            _vendorcontactRepository = vendorcontactRepository;
        }
        /// <summary>
        /// 修改客户联系编辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(VendorcontactUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result();
            try
            {
                var vendorcontactUpdate = await (await _vendorcontactRepository.GetAll()).Where(c => c.VendorcontactId == request.VendorcontactId).FirstOrDefaultAsync();
                if (vendorcontactUpdate != null)
                {
                    vendorcontactUpdate.UpdatedOwnerId = "张三";
                    vendorcontactUpdate.UpdatedTime = DateTime.Now;
                    var res = await _vendorcontactRepository.Update(vendorcontactUpdate);
                    if (res > 0)
                    {
                        result.Code = (int)CommonErrorCode.Success;
                        result.Msg = "修改成功";
                    }
                    else
                    {
                        result.Msg = "修改失败";
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
